package com.bys.managercolok;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;

import java.util.Calendar;

public class AlarmManagerUtil {
    private static final String ALARM_ACTION = "com.bys.alarm.clock";

    /**
     *
     * @param context 显示布局
     * @param flag 周期性时间间隔的标志
     * @param hour 时
     * @param minute 分
      * @param id 闹钟的 id
     * @param week   week=0表示一次性闹钟或者按天的周期性闹钟  ，非0 的情况下是几就代表以周为周期性的周几的闹钟
     * @param tips 闹钟提示的信息
     * @param soundOrVibrator  表示声音和震动都执行，1表示只有铃声提醒，0表示只有震动提醒
     */
    public static void setAlarm(Context context,int flag,int hour,int minute,int id,int week,String tips,int soundOrVibrator){
        AlarmManager alManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Calendar calendar = Calendar.getInstance();
//        间隔毫秒
        long intervalMillis = 0;
        calendar.set(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH),hour,minute,0);
        if(flag == 0){
//            每天的闹钟
            intervalMillis = 0;
        }else if(flag == 1){
//            间隔一天
            intervalMillis = 1000 * 60 * 60 *24;
        }else if (flag == 2){
//            间隔一周
            intervalMillis = 1000 * 60 * 60 * 24 * 7;
        }
        Intent intent = new Intent(ALARM_ACTION);
        intent.putExtra("inttervalMillis",intervalMillis);
        intent.putExtra("msg",tips);
        intent.putExtra("id",id);
        intent.putExtra("soundOrVibrator",soundOrVibrator);
//        getBroadcast 获取广播
//        PendingIntent.FLAG_CANCEL_CURRENT 如果AlarmManager管理的PendingIntent已经存在，那么将会取消当前的PendingIntent，从而创建一个新的PendingIntent
//        2.FLAG_UPDATE_CURRENT 如果AlarmManager管理的PendingIntent已经存在，可以让新的Intent更新之前PendingIntent中的Intent对象数据
//        FLAG_NO_CREATE  如果AlarmManager管理的PendingIntent已经存在，那么将不进行任何操作，直接返回已经存在的PendingIntent
        PendingIntent sender =PendingIntent.getBroadcast(context,id,intent,PendingIntent.FLAG_CANCEL_CURRENT);
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
            alManager.setWindow(AlarmManager.RTC_WAKEUP,calMethod(week,calendar.getTimeInMillis()),intervalMillis,sender);
        }


    }

    /**
     *
     * @param week 传入的周几
     * @param dateTime  传入的是时间戳（设置当天的年月日+从选择框拿来的时分秒）
     * @return  返回起始闹钟时间的时间戳
     */
    private static long calMethod(int week, long dateTime) {
        long time = 0;
//        如果week 是 0 表示每天或者只响一次
        if(week  != 0){
            Calendar c = Calendar.getInstance();
            int weekNow = c.get(Calendar.DAY_OF_WEEK);
            if(1 == weekNow){
                weekNow = 7;
            }else if (2 == weekNow)
                weekNow = 1;
            else if(3 == weekNow)
                weekNow = 2;
            else if (4 == weekNow)
                weekNow = 3;
            else if (5 == weekNow)
                weekNow = 4;
            else if (6 == week)
                weekNow = 5;
            else if (7 == week)
                weekNow = 6;
            if(weekNow == week){
                if(dateTime > System.currentTimeMillis()){
                    time = dateTime;
                }else {
                    time = dateTime + 1000 * 60 * 60 * 24 * 7;
                }
            }else if (week > weekNow){

            }else if (week < weekNow){

            }

            
        }else{
//            如果设置的时间大于 系统的时间
            if(dateTime > System.currentTimeMillis())
                time = dateTime;
            else
                time = dateTime + 1000 * 60 * 60 * 24;
        }
        return  time;
    }


    public static void setAlarmTime(Context context, long timeInMillis, Intent intent) {
        AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        PendingIntent sender = PendingIntent.getBroadcast(context, intent.getIntExtra("id", 0),intent,PendingIntent.FLAG_CANCEL_CURRENT);
        long interval = intent.getLongExtra("intervalMillis", 0);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            manager.setWindow(AlarmManager.RTC_WAKEUP,timeInMillis,interval,sender);
        }
    }
}
